import {createRouter, createWebHistory} from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    redirect: '/login/loginUser',
    meta: {
      requireLogin: false
    },
    component: () => import('@/views/login/login'),
    children: [
      {
        path: 'loginUser',
        name: 'loginUser',
        component: () => import('@/views/login/loginUser')
      },
      {
        path: 'loginMobile',
        name: 'loginMobile',
        component: () => import('@/views/login/loginMobile')
      },
      {
        path: 'loginModifyPass',
        name: 'loginModifyPass',
        component: () => import('@/views/login/loginModifyPass'),
      },
      {
        path: 'loginBindingPass',
        name: 'loginBindingPass',
        component: () => import('@/views/login/loginBindingPass')
      }
    ]
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/home'),
    meta: {
      requireLogin: false,
    }
  },
  {
    path: '/backstageLogin',
    name: 'backstageLogin',
    component: () => import("@/views/backstage/login/index"),
    meta: {
      requireLogin: false
    },
  },
  {
    path: '/backstage',
    name: 'backstage',
    component: () => import("@/views/backstage/frame/index"),
    meta: {
      requireLogin: true,
    },
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import("@/views/backstage/user/index"),
      },
      {
        path: 'corporation',
        name: 'corporation',
        component: () => import("@/views/backstage/corporation/index"),
      },
      {
        path: 'allWeekly',
        name: 'allWeekly',
        component: () => import("@/views/backstage/allWeekly/index"),
        children: [
          {
            path: "editArea",
            name: "editArea",
            component: () => import("@/components/editArea"),
          }
        ]
      },
      {
        path: 'schedule',
        name: 'schedule',
        component: () => import("@/views/backstage/schedule/index"),
      },
      {
        path: 'banner',
        name: 'banner',
        component: () => import("@/views/backstage/banner/index"),
      },
      {
        path: 'setNav',
        name: 'setNav',
        component: () => import("@/views/backstage/setNav/index"),
      },
      {
        path: 'nodeControl',
        name: 'nodeControl',
        component: () => import("@/views/backstage/nodeControl/index"),
      },
      {
        path: 'function',
        name: 'function',
        component: () => import("@/views/backstage/function/index"),
      },
      {
        path: 'semester',
        name: 'semester',
        component: () => import("@/views/backstage/semester/index"),
      },
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404/404'),
  },
  {
    path: '/weekly',
    name: 'weekly',
    meta: {
      requireLogin: false
    },
    component: () => import('@/views/weekly/weekly'),
    redirect: '/weekly/weeklySearch',
    children: [
      {
        path: 'weeklySearch',
        name: 'weeklySearch',
        component: () => import('@/views/weekly/weeklySearch'),
      },
      {
        path: 'weeklyEdit',
        name: 'weeklyEdit',
        component: () => import('@/views/weekly/weeklyEdit'),
      },
      {
        path: 'weeklyContent/:id',
        name: 'weeklyContent',
        component: () => import('@/views/weekly/weeklyContent'),
      },
    ]
  },
  {
    path: '/study',
    name: 'study',
    component: () => import('@/views/study/study'),
    redirect: '/study/studyTime',
    meta: {
      requireLogin: true
    },
    children: [
      {
        path: 'studyTime',
        name: 'studyTime',
        component: () => import('@/views/study/studyTime')
      },
      {
        path:'studySchedule',
        name:'studySchedule',
        component:()=>import('@/views/study/studySchedule')
      },
      {
        path:'uploadProgress',
        name:'uploadProgress',
        component:()=>import('@/views/study/uploadProgress')
      }
    ]
  },
]


const router = new createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})

import $store from '@/store'

router.beforeEach((to, from, next) => {
  let store = $store.state,
    isLogin = store.userInfo.isLogin;

  if (to.matched.length === 0) {
    next({name: '404'})
  } else {
    if (to.meta.requireLogin) {
      if (isLogin) {
        next()
      } else if (to.path.includes("backstage")) {
        next({name: 'backstageLogin'})
      } else {
        next({name: 'login'})
      }
    } else {
      next()
    }
  }
})


export default router
